/*
 * Copyright (c) 2022  by Junzhao
 * THIS FILE IS PART OF JAVASE Song Huongkang PROJECT
 * All Rights Reserved.
 */

package 继承Thread类;

import javax.security.auth.Subject;

/**
 * @description 多线程创建方式 继承Thread类
 *      创建一个Thread的子类
 *      重写run（）方法 --->将此线程执行的操作写在其中
 *      创建其实例
 *      对象调用start（）方法
 *
 * @author 俊昭
 * @date 2022/4/12
*/
public class ThreadTest {
    public static void main(String[] args) {
        SubThread st = new SubThread();
//        st.run();//如果这个线程是使用一个单独的Runnable运行对象构造的，那么这个Runnable对象的run方法就会被调用;否则，此方法不执行任何操作并返回
        st.start();// 启动线程 调用线程的run
//        再启动一个线程
//        st.start();//  非法的线程状态 IllegalThreadStateException

        SubThread st1 = new SubThread();
        st1.start();
        // main线程中执行
        for (int i = 0 ;i<100;i++){
            if (i % 2 == 1){
                System.out.println(i+"*");
            }
        }
    }
}
class SubThread extends Thread{
    @Override
    public void run() {
        // 分线程
        for (int i = 0 ;i<100;i++){
            if (i % 2 == 0){
                System.out.println(i+" "+Thread.currentThread().getName());
            }
        }
//        System.out.println("hello ");
    }
}
/*
1*
3*
5*
7*
9*
11*
13*
15*
17*
19*
21*
23*
25*
27*
29*
31*
33*
35*
37*
39*
41*
43*
45*
47*
49*
51*
53*
55*
57*
59*
61*
63*
65*
67*
69*
71*
73*
75*
0 Thread-1
2 Thread-1
4 Thread-1
6 Thread-1
8 Thread-1
10 Thread-1
0 Thread-0
12 Thread-1
14 Thread-1
16 Thread-1
18 Thread-1
20 Thread-1
22 Thread-1
24 Thread-1
26 Thread-1
28 Thread-1
30 Thread-1
32 Thread-1
34 Thread-1
36 Thread-1
38 Thread-1
40 Thread-1
42 Thread-1
44 Thread-1
46 Thread-1
48 Thread-1
50 Thread-1
52 Thread-1
54 Thread-1
56 Thread-1
58 Thread-1
60 Thread-1
62 Thread-1
64 Thread-1
66 Thread-1
68 Thread-1
70 Thread-1
72 Thread-1
74 Thread-1
76 Thread-1
78 Thread-1
80 Thread-1
82 Thread-1
84 Thread-1
86 Thread-1
88 Thread-1
90 Thread-1
92 Thread-1
94 Thread-1
96 Thread-1
98 Thread-1
77*
79*
81*
83*
85*
87*
89*
91*
93*
95*
97*
99*
2 Thread-0
4 Thread-0
6 Thread-0
8 Thread-0
10 Thread-0
12 Thread-0
14 Thread-0
16 Thread-0
18 Thread-0
20 Thread-0
22 Thread-0
24 Thread-0
26 Thread-0
28 Thread-0
30 Thread-0
32 Thread-0
34 Thread-0
36 Thread-0
38 Thread-0
40 Thread-0
42 Thread-0
44 Thread-0
46 Thread-0
48 Thread-0
50 Thread-0
52 Thread-0
54 Thread-0
56 Thread-0
58 Thread-0
60 Thread-0
62 Thread-0
64 Thread-0
66 Thread-0
68 Thread-0
70 Thread-0
72 Thread-0
74 Thread-0
76 Thread-0
78 Thread-0
80 Thread-0
82 Thread-0
84 Thread-0
86 Thread-0
88 Thread-0
90 Thread-0
92 Thread-0
94 Thread-0
96 Thread-0
98 Thread-0

进程已结束，退出代码为 0

*/